<?php
namespace app\admin\controller;
use think\Request;
class Member extends Common
{
    public function user_list()
    {
        $search=input('get.search','','htmlspecialchars');
        $page_search=$search;
        $pagenum=4;
        $db=db('menber');
        if($search){
        
            $db->where('user|phone','like','%'.$search.'%');
            
            // ini_set('memory_limit','1024M');
       
            // require EXTEND_PATH.'jieba/vendor/multi-array/MultiArray.php';
            // require EXTEND_PATH.'jieba/vendor/multi-array/Factory/MultiArrayFactory.php';
            // require EXTEND_PATH.'jieba/class/Jieba.php';
            // require EXTEND_PATH.'jieba/class/Finalseg.php';
        
            // \Fukuball\Jieba\Jieba::init();
            // \Fukuball\Jieba\ Finalseg::init();
            // $search = \Fukuball\Jieba\Jieba::cut($search);
         
            // for ($i = 0; $i<count($search) ; $i++){
            //     $search[$i] = '%'.$search[$i].'%';
            // }

            // $where = '';
            // for ($i = 0; $i<count($search) ; $i++){
            //     $where .= ' `ord` LIKE "'.$search[$i]. '" OR ';
            // }
            // $where = rtrim($where , 'OR ');
            // $db->where($where);
    
        }
        $message=$db->alias('a')->field('a.*,b.name as groupname')->join('group b','a.group=b.id')->paginate($pagenum,false,['query'=>['search'=>$page_search]]);
        $page=$message->render();
        $page=str_replace(
            ['class="pagination"','class="disabled"','class="active"'],
            ['class="am-pagination tpl-pagination"','class="am-disabled"','class="am-active"'],
            $page
        );
        $current_page=input('get.page','1','intval');
        $request=Request::instance();
        if($request->isAjax()){
            return json(['message'=>$message,'page'=>$page,'current_page'=>$current_page]);
        }
        $sum=db('menber')->count();
        $this->assign('page' , $page);
        $this->assign('message',$message);
        $this->assign('current_page' , $current_page);
        return view();
    }
    public function delect(){
        $id=input('get.id');
        db('menber')->delete($id);
    }
    public function edit_user(){
        $id=input('get.id');
        $group=db('group')->select();
        $res=db('menber')->alias('a')->field('a.*,b.name as groupname')->join('group b','a.group=b.id')->where('a.id',$id)->find();
        $this->assign('res',$res);
        $this->assign('group',$group);
        return view();
    }
    public function user_edit(){
        $post=input('post.');
        $result=db('menber')->where('id',$post['id'])->update($post);
        if($result){
            $this->success('更新成功' , url('member/user_list') );
        }
    }
    public function add_user()
    {
        $group=db('group')->select();
        $this->assign('group',$group);
        return view();
    }
    public function user_add(){
        $post=input('post.');
        $post['user']=input('post.user','','htmlspecialchars');
        $post['pwd']=md5(input('post.pwd','','trim'));
        $post['inputtime']=date('Y-m-d h:m:s');
        $member=validate('member');           
        if(!$member->check($post)){
            $this->error($member->getError());
        }
        $id=db('menber')->insert($post);
        if($id){
            $this->success('添加成功',url('user_list'));
        }else{
            $this->error('添加失败');
        }
    }
    public function user_rank()
    {
        return view();
    }
    public function user_message()
    {
        return view();
    }
    public function reply_message()
    {
        return view();
    }
    public function add_user_rank(){
        return view();
    }
}
